from PyQt5.Qt import *
import math
import random
import time
import sys
from jiemian import Ui_MainWindow

class PaintArea(QWidget):#画图类
    def __init__(self,parent = None, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.setPalette(QPalette(QColor(240,240,240)))#设置背景颜色
        self.setAutoFillBackground(True)#设置窗口自动填充背景
        self.car = 0
    def paintEvent(self, event):#重写绘图事件
        self.rect_base = QRect(20,20,self.width()-40,self.width()-40)#设置了画图的区域
        self.painter_base = QPainter()#底图绘图类
        self.painter_base.begin(self)
        #底图,边框
        self.painter_base.setBrush(QColor(100,100,255))
        self.painter_base.setPen(Qt.black)
        self.painter_base.drawLine(40, 120, 440, 120)
        self.painter_base.drawLine(40, 120, 40, 410)
        self.painter_base.drawLine(40, 410, 440, 410)
        self.painter_base.drawLine(440, 120, 440, 410)
        #地图
        self.painter_base.setPen(QPen(QColor(0, 0, 0), 4, Qt.SolidLine))
        self.painter_base.drawLine(80, 380, 80, 250)
        self.painter_base.drawLine(80, 250, 120, 250)
        self.painter_base.drawArc(118, 220, 100, 100, 0, 2500)
        self.painter_base.drawLine(218, 270, 218, 350)
        self.painter_base.drawLine(218, 350, 328, 350)
        self.painter_base.drawLine(328, 150, 328, 350)
        self.painter_base.end()


    #小车
        self.painter = QPainter()  # 画图类
        self.painter.begin(self)
        self.painter.setPen(QPen(QColor(0, 0, 255), 2, Qt.SolidLine))
        self.painter.drawRect(73, 380-self.car,14, 20)
        self.painter.setPen(QPen(QColor(255, 0, 0), 2, Qt.SolidLine))
        self.painter.drawLine(73, 380-self.car, 87, 380-self.car)  # 车头
        self.painter.end()  # 结束
    #得到小车坐标
    def setCar(self, car):
        self.car=car

class Window(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setup_ui()
        self.setupUi(self)

        # 定时器
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.get_car)
        self.timer.start(1000)


    def setup_ui(self):
        #子控件加这里
        self.area = PaintArea(self)#添加画图控件
        self.area.resize(450,450)#设置大小
        self.area.move(10,10)#设置位置

    def get_car(self):
        car=10
        self.area.setCar(car)
        self.area.update()





if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
